const { ResultSuccess } = require("./utils/utils");
const express = require("express");
const app = express();
var bodyParser = require("body-parser");
const { v4: uuidv4 } = require("uuid");

const generate = require("./utils/generateProTable");

// 解决跨域问题
app.all("*", function (req, res, next) {
  // 设置允许跨域的域名,*代表允许任意域名跨域
  res.header("Access-Control-Allow-Origin", "*");
  // 允许的header类型
  res.header("Access-Control-Allow-Headers", "content-type");
  // 跨域允许的请求方式
  res.header("Access-Control-Allow-Methods", "DELETE,PUT,POST,GET,OPTIONS");
  if (req.method.toLowerCase() == "options") res.send(200); // 让options 尝试请求快速结束
  else next();
});

//在原有的基础上加上下面代码即可
app.use(
  bodyParser.json({
    limit: "50mb"
  })
);
app.use(
  bodyParser.urlencoded({
    limit: "50mb",
    extended: true
  })
);

/**
 * current:当前页
 */
const generateDate = (current, pageSize) => {
  const dataList = [];
  for (let i = pageSize * (current - 1); i < current * pageSize; i++) {
    // i 从0开始
    dataList.push({
      id: i + 1,
      name: "赵先生" + (i + 1),
      age: 19,
      phone: 18700871300,
      state: i % 2 === 0 ? "drop" : "normal", // 正常 / 退学
      address: "陕西省西安市雁塔区",
      height: "183",
      weight: "72",
      education: "西安交通大学",
      createTime: Date.now()
    });
  }
  return dataList;
};
const pageData = (dataList, current, pageSize) => {
  const newDataList = [];
  for (let i = pageSize * (current - 1); i < current * pageSize; i++) {
    newDataList.push(dataList[i]);
  }
  return newDataList;
};
// 模拟响应的数据
let tableDataList = generateDate(1, 999);

app.post("/selectList", async (req, res) => {
  let { current, pageSize } = req.body;
  if (!current) current = 1;
  if (!pageSize) pageSize = 20;

  let tableDataListTemp = JSON.parse(JSON.stringify(tableDataList));
  tableDataListTemp = tableDataListTemp.map((item) => {
    delete item.address;
    delete item.height;
    delete item.weight;
    delete item.education;
    return item;
  });
  tableDataListTemp = pageData(tableDataListTemp, current, pageSize);
  return ResultSuccess(res, "成功", { list: tableDataListTemp, total: tableDataList.length });
});
app.get("/selectDetail", async (req, res) => {
  const query = req.query;
  // console.debug(req)
  // 模拟响应的数据
  const tableDataDetail = {
    ...tableDataList.find((item) => {
      return item.id + "" === query.id + "";
    })
  };
  if (!tableDataDetail || Object.keys(tableDataDetail).length === 0) {
    return ResultSuccess(res, "成功", {});
  }
  return ResultSuccess(res, "成功", tableDataDetail);
});
app.get("/delete", async (req, res) => {
  const query = req.query;
  tableDataList = tableDataList.filter((item) => {
    return item.id != query.id;
  });
  return ResultSuccess(res, "成功", {});
});
app.post("/add", async (req, res) => {
  const body = req.body;
  console.debug("----------add", body, uuidv4());
  // tableDataList = tableDataList.filter((item) => {
  //   return item.id != query.id;
  // });
  return ResultSuccess(res, "成功", {});
});
app.post("/update", async (req, res) => {
  const body = req.body;
  console.debug("----------update", body, uuidv4());
  tableDataList = tableDataList.map((item) => {
    if (item.id === body.id) {
      item = body;
    }
    return item;
  });
  return ResultSuccess(res, "成功", {});
});
app.post("/generate", async (req, res) => {
  // console.debug("generate", req.body);
  try {
    return generate.generateProTable(res, req.body);
  } catch (e) {
    console.error(e);
  }
});

// 监听
const port = 8081;
app.listen(port, function () {
  console.debug("success listen..." + port);
});
